<!--
 * @Description: 一卡通专区商品列表
 * @Author: Li Yujie
 * @Date: 2020/12/22
 * @Last Modified by: Li Yujie
 * @Last Modified time: 2021/01/09
-->
<template>
    <PageLayout title="一卡通专区">
        <template slot="filter">
            <!-- 筛选-->
            <div class="left">
                <div class="filter-item">
                    <div class="filter-item__title">商品信息</div>
                    <el-input
                        placeholder="请输入商品编号/商品名称"
                        v-model="filterData.keyword"
                        suffix-icon="el-icon-search"
                        clearable
                        size="medium"
                        style="width: 300px;"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="filter-item">
                    <div class="filter-item__title">上架状态</div>
                    <el-select
                        v-model="filterData.goods_status_vip"
                        clearable
                        filterable
                        placeholder="全部"
                        size="medium"
                        style="width: 203px"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in statusList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"/>
                    </el-select>
                </div>
                <div class="filter-item">
                    <el-button
                        type="primary"
                        size="small"
                        class="query-button"
                        @click="getTableData(1)"
                    >
                        查询
                    </el-button>
                </div>
            </div>
            <div class="btn-view">
                <el-button
                    type="primary"
                    size="medium"
                    @click="exportShop()"
                >
                    导入商品
                </el-button>
                <el-button
                    type="primary"
                    size="medium"
                    @click="$refs.goodsSelect.show(2, requestUrl, typeList)"
                >
                    批量添加商品
                </el-button>
                <el-button
                    type="primary"
                    size="medium"
                    @click="handleGoodsEdit('', 'add')"
                >
                    添加一卡通专区商品
                </el-button>
            </div>
        </template>
        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            ref="table"
            class="data-table"
            v-loading="tableLoading"
        >
            <el-table-column
                type="index"
                label="序号"
                width="60"
                align="center"
            />
            <el-table-column
                label="商品信息"
                width="300"
            >
                <template slot-scope="scope">
                    <div class="goods-info">
                        <div class="goods-cover">
                            <img
                                :src="scope.row.goods_cover"
                                alt=""
                                @click="fnShowPreview(scope.row.goods_cover)"
                            >
                        </div>
                        <div class="goods-content">
                            <el-tooltip
                                effect="dark"
                                :content="scope.row.goods_title"
                                placement="top"
                                :disabled="scope.row.goods_title.length < 23"
                            >
                                <div
                                    class="goods-title"
                                    @click="toGoodsList(scope.row.goods_title)"
                                >
                                    {{ scope.row.goods_title }}
                                </div>
                            </el-tooltip>
                            <div class="goods-no">
                                商品ID：{{ scope.row.goods_no }}
                            </div>
                        </div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="goods_price"
                label="学生价"
                show-overflow-tooltip
                align="center"
            >
                <template slot-scope="scope">
                    ￥{{ scope.row.goods_price }}
                </template>
            </el-table-column>
            <el-table-column
                label="团购价"
                show-overflow-tooltip
                align="center"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        @click="getGroupPrice(scope.row)"
                    >
                        查看
                    </el-button>
                </template>
            </el-table-column>
            <el-table-column
                label="原佣金"
                show-overflow-tooltip
                align="center"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        @click="getProductList(scope.row)"
                    >
                        查看
                    </el-button>
                </template>
            </el-table-column>
            <el-table-column
                label="活动佣金"
                show-overflow-tooltip
                align="center"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        @click="getProductList(scope.row)"
                    >
                        查看
                    </el-button>
                </template>
            </el-table-column>
            <el-table-column
                prop="goods_discount_vip"
                label="一卡通折扣"
                show-overflow-tooltip
                align="center"
                width="110"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    {{ scope.row.goods_discount_vip ? `${scope.row.goods_discount_vip}折`: '--' }}
                </template>
            </el-table-column>
            <el-table-column
                prop="goods_price_vip"
                label="一卡通价"
                show-overflow-tooltip
                align="center"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    {{ scope.row.goods_price_vip ? `￥${scope.row.goods_price_vip}`: '--' }}
                </template>
            </el-table-column>
            <el-table-column
                prop="goods_sort_vip"
                label="排序号"
                show-overflow-tooltip
                align="center"
                :formatter="emptyFormatter"
            />
            <el-table-column
                label="显示区域"
                show-overflow-tooltip
                align="center"
            >
                <template slot-scope="scope">
                    <div v-if="scope.row.category === 0">一卡通专区</div>
                    <div v-else-if="scope.row.category === 1">热销专区</div>
                    <div v-else>--</div>
                </template>
            </el-table-column>
            <el-table-column
                label="添加时间"
                show-overflow-tooltip
                width="160"
            >
                <template slot-scope="scope">
                    <div>{{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }}</div>
                </template>
            </el-table-column>
            <el-table-column
                label="每月限购"
                show-overflow-tooltip
                align="center"
            >
                <template slot-scope="scope">
                    <div>{{ scope.row.goods_buy_limit_vip ? scope.row.goods_buy_limit_vip : '不限' }}</div>
                </template>
            </el-table-column>
            <el-table-column
                label="是否平台补贴"
                show-overflow-tooltip
                align="center"
                width="110"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        @click="getProductList(scope.row)"
                    >
                        查看
                    </el-button>
                </template>
            </el-table-column>
            <el-table-column
                label="上架状态"
                show-overflow-tooltip
                align="center"
            >
                <template slot-scope="scope">
                    <div class="status-wrapper">
                        <div :class="`status-dot ${scope.row.goods_status_vip === 1 ? 'success' : 'danger'}`"/>
                        <div v-if="scope.row.goods_status_vip === 1">已上架</div>
                        <div v-else-if="scope.row.goods_status_vip === 0">已下架</div>
                        <div v-else>--</div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                width="160"
                align="left"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        @click="handleGoodsEdit(scope.row.goods_no, 'edit')"
                    >
                        编辑
                    </el-button>
                    <el-button
                        type="text"
                        @click="handleGoodsOption(scope.row)"
                    >
                        {{ scope.row.goods_status_vip === 1 ? '下架' : '上架' }}
                    </el-button>
                    <el-button
                        type="text"
                        @click="handleGoodsDelete(scope.row.goods_no)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->
        <!-- /分页 -->
        <template slot="pagination">
            <div/>
            <el-pagination
                layout="total, prev, pager, next, jumper"
                background
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </template>
        <!--商品选择弹窗-->
        <goods-select
            ref="goodsSelect"
            @change="handleSelectGoods"
        />
        <!-- 团购价弹窗 -->
        <group-price-dialog
            ref="groupPriceDialog"
        />
        <!-- 导入商品 -->
        <goods-import
            @change="handleSelectGoods($event.list)"
            :visible.sync="goodsImportDialog"
            :request-url="requestUrl"
        />
        <!-- 图片预览 -->
        <img-preview ref="preview"/>
        <!-- 查看弹窗 -->
        <el-dialog
            title="查看详情"
            :visible.sync="productVisible"
            width="800px"
            :close-on-click-modal="false"
        >
            <el-table
                :data="productList"
                height="100%"
                style="width: 100%;max-height: 450px"
                class="data-table"
            >
                <el-table-column
                    width="240"
                    label="规格"
                >
                    <template slot-scope="scope">
                        {{ scope.row.spec_list | filterSpec }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="原佣金"
                    show-overflow-tooltip
                    align="center"
                >
                    <template slot-scope="scope">
                        {{ scope.row.goods_commission_rate }}%
                    </template>
                </el-table-column>
                <el-table-column
                    label="活动佣金"
                    show-overflow-tooltip
                    align="center"
                >
                    <template slot-scope="scope">
                        {{ scope.row.commission_rate }}%
                    </template>
                </el-table-column>
                <el-table-column
                    property="name"
                    align="center"
                    label="是否平台补贴"
                >
                    <template slot-scope="scope">
                        <div v-if="scope.row.shop_subsidy_enable === 1">平台补贴</div>
                        <div v-if="scope.row.shop_subsidy_enable === 0">商家全部补贴</div>
                        <div v-if="scope.row.shop_subsidy_enable === 2">商家部分补贴</div>
                    </template>
                </el-table-column>
                <el-table-column
                    property="shop_subsidy_money_by_shop"
                    align="center"
                    label="商家补贴金额"
                    :formatter="emptyFormatter"
                />
            </el-table>
            <span
                slot="footer"
                class="dialog-footer"
            >
                <el-button
                    @click="productVisible = false"
                    size="medium"
                    type="primary"
                >
                    我知道了
                </el-button>
            </span>
        </el-dialog>
    </PageLayout>
</template>

<script>
import {back} from '@/mixin/back';
import GoodsSelect from '@/components/goods/goods-select';
import GroupPriceDialog from '@/components/member-card/group-price-dialog';
import GoodsImport from "@/components/goods/goods-import";
import ImgPreview from "@/components/common/img-preview";

export default {
    name: "MemberAreaList",
    data() {
        return {
            filterData: {
                keyword: '',
                goods_status_vip: ''
            },
            tableData: [],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            tableLoading: false,
            statusList: [
                {label: '全部', value: ''},
                {label: '已上架', value: 1},
                {label: '已下架', value: 0}
            ],
            // 商品弹窗类型
            typeList: [
                {label: '自营', value: 1},
                {label: '折扣', value: 5},
                {label: '秒杀', value: 3}
            ],
            requestUrl: '/student/shop_vip_goods%5Cget_depot_goods_list',
            authResult: {},
            goodsImportDialog: false,
            productList: [],
            productVisible: false,
        };
    },
    mixins: [back],
    components: {
        ImgPreview,
        GoodsSelect,
        GroupPriceDialog,
        GoodsImport,
    },
    filters: {
        filterSpec (val) {
            let str = '';

            val.forEach((el, n) => {
                str += el.spec_name + ':' + el.spec_value + (n == val.length - 1 ? '' : ';');
            });

            return str;
        }
    },
    mounted() {
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '商城'}, {title: '一卡通专区'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },
    methods: {

        /**
         * 获取表格数据
         * @param page
         */
        getTableData(page) {
            this.tableLoading = true;
            let params = {...this.filterData};

            params.page_id = page || this.page.now_page;
            this.$post('/student/shop_vip_goods%5Cget_list', params, resp => {
                if (resp.code === 1) {
                    this.tableData = resp.data.list;
                    this.page = resp.data.page;
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
                this.tableLoading = false;
            });
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 跳转编辑商品
         * @param no
         * @param type add 新增 edit 编辑
         */
        handleGoodsEdit(no = '', type) {

            this.$router.push({
                path: '/student/member-goods-edit',
                query: {
                    no,
                    type
                }
            });
        },

        /**
         * 删除专区商品
         * @param no
         */
        handleGoodsDelete(no) {
            if (!this.authResult.auth_delete) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.$zdConfirm('此操作将删除该商品, 是否继续?', '温馨提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = {
                    goods_no: no
                };

                this.$post('/student/shop_vip_goods%5Cdelete', params, resp => {
                    if (resp.code === 1) {
                        this.$message.success('删除成功');
                        this.getTableData();
                    } else {
                        this.$notify({
                            title: '失败',
                            message: resp.msg,
                            duration: 2000,
                            type: 'warning'
                        });
                    }
                });
            }).catch(() => {});
        },

        /**
         * 查看购买详情
         * @param no
         */
        toOrderDetail(no) {

            let routeData = this.$router.resolve({
                path: '/student/order-list',
                query: {}
            });

            window.open(routeData.href, '_blank');
        },

        /**
         * 获取批量选择的商品
         * @param val
         */
        handleSelectGoods(val) {
            if(val.length === 0) {
                this.$message.warning('批量设置商品为空');
                return;
            }
            this.$router.push({
                path: '/student/member-goods-edit-batch',
                query: {
                    goodsList: JSON.stringify(val)
                }
            });
        },

        /**
         * 商品上下架
         * @param row
         */
        handleGoodsOption(row) {
            if (!this.authResult.auth_modify) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.$zdConfirm(`确认${row.goods_status_vip === 1 ? '下架' : '上架'}该商品吗?`, '温馨提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = {
                    goods_no: row.goods_no,
                    goods_status_vip: row.goods_status_vip === 1 ? 0 : 1
                };

                this.$post('/student/shop_vip_goods%5Cmodify', params, resp => {
                    if (resp.code === 1) {
                        this.$message.success('操作成功');
                        this.getTableData();
                    } else {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            duration: 2000,
                            type: 'warning'
                        });
                    }
                });
            }).catch(() => {});
        },

        /**
         * 跳转商品列表
         * @param title
         */
        toGoodsList(title) {
            let routeData = this.$router.resolve({
                name: 'GoodsList',
                query: {
                    goods_title: title,
                    goods_type: 'self'
                }
            });

            window.open(routeData.href, '_blank');
        },

        /**
         * 获取商品团购价格列表
         * @param row
         */
        getGroupPrice(row) {

            let params = {
                goods_no: row.goods_no,
            };

            this.$post('/student/shop_vip_goods%5Cget_product_list', params, resp => {
                if (resp.code === 1) {
                    if(!resp.data.wholesale_info) {
                        this.$message.warning('此商品无团购价格');
                        return;
                    }
                    let product_list = resp.data.product_list;
                    let discountList = resp.data.wholesale_info.list;
                    let result = [];

                    for (let i = 0; i < product_list.length; i++) {
                        let spec_info = '';
                        let product_price = product_list[i].product_price;

                        // 规格处理
                        product_list[i].spec_list.forEach(spec => {
                            spec_info += `${spec.spec_name}:${spec.spec_value}； `;
                        });

                        result.push({
                            spec_list: spec_info,
                            goods_price: product_price,
                            data: discountList
                        });
                    }
                    this.$refs.groupPriceDialog.show(result);

                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * 获取规格数据
         * @param row
         */
        getProductList(row) {
            let params = {
                goods_no: row.goods_no,
            };

            this.$post('/student/shop_vip_goods%5Cget_product_list', params, resp => {
                if (resp.code === 1) {
                    this.productList = resp.data.product_list;
                    this.productList.forEach(item => {
                        item.goods_commission_rate = row.goods_commission_rate;
                        if(Number(item.commission_rate) === -1) {
                            item.commission_rate = item.goods_commission_rate;
                        }
                    });
                    this.productVisible = true;
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * @description 导入商品
         */
        exportShop () {
            this.goodsImportDialog = true;
        },

        /**
         * 查看图片
         * @param   {String}    images      //单图预览
         * @param   {Array}     images      //多图预览
         */
        fnShowPreview(images) {
            if (images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images);
            } else {
                this.$message({
                    type: 'warning',
                    message: '图片类型错误'
                });
            }
        },
    }
};
</script>

<style scoped lang="less">
/deep/ .filter-wrapper {
    justify-content: space-between;
    .left {
        display: flex;
    }
    .btn-view {
        margin-top: -22px;
    }
}
.goods-info {
    height: 80px;
    margin: 8px 0;
    display: flex;
    .goods-cover {
        width: 80px;
        height: 80px;
        margin-right: 10px;
        flex-shrink: 0;
        img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            cursor: pointer;
        }
    }
    .goods-content {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .goods-title {
            font-size: 14px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #20A0FF;
            cursor: pointer;
            line-height: 20px;
            overflow: hidden;
            text-overflow: ellipsis;
            display:-webkit-box;
            -webkit-line-clamp:2;
            /*! autoprefixer: off */
            -webkit-box-orient: vertical;
        }
    }
}
.dialog-footer {
    display: flex;
    justify-content: center;
}
</style>
